<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMenuUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('menus',function(Blueprint $table){
            $table->increments('id');
            $table->string('name');
            $table->string('label')->nullable();
            $table->string('icon')->nullable();
            $table->string('action')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });


        Schema::create('permissions', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('label')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('role_menu',function(Blueprint $table){
            $table->integer('role_id')->unsigned();
            $table->integer('menu_id')->unsigned();
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('role_id')
                  ->references('id')
                  ->on('roles')
                  ->onDelete('cascade');

            $table->foreign('menu_id')
                  ->references('id')
                  ->on('menus')
                  ->onDelete('cascade');

            $table->primary(['role_id','menu_id']);
        });

        Schema::create('role_permission',function(Blueprint $table){
            $table->integer('role_id')->unsigned();
            $table->integer('permission_id')->unsigned();
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('role_id')
                  ->references('ID')
                  ->on('roles')
                  ->onDelete('cascade');

            $table->foreign('permission_id')
                  ->references('id')
                  ->on('permissions')
                  ->onDelete('cascade');

            $table->primary(['permission_id','role_id']);

        });

        Schema::create('role_user',function(Blueprint $table){
            $table->integer('role_id')->unsigned();
            $table->integer('user_id')->unsigned();
            $table->timestamps();
            $table->softDeletes();
            $table->foreign('user_id')
                  ->references('ID')
                  ->on('user')
                  ->onDelete('cascade');

            $table->foreign('role_id')
                  ->references('id')
                  ->on('roles')
                  ->onDelete('cascade');

            $table->primary(['user_id', 'role_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // drop foreign keys
        Schema::table('role_menu',function($table){
          $table->dropForeign('role_menu_role_id_foreign');
          $table->dropForeign('role_menu_menu_id_foreign');
        });
        Schema::table('role_permission',function($table){
          $table->dropForeign('role_permission_permission_id_foreign');
          $table->dropForeign('role_permission_role_id_foreign');
        });
        Schema::table('role_user',function($table){
          $table->dropForeign('role_user_user_id_foreign');
          $table->dropForeign('role_user_role_id_foreign');
        });

        Schema::drop('role_menu');
        Schema::drop('role_permission');
        Schema::drop('role_user');
        Schema::drop('menus');
        Schema::drop('permissions');
    }
}
